//
//  YCWXApiUtils.m
//  YouCabService
//
//  Created by gml on 2018/11/29.
//  Copyright © 2018年 NQ. All rights reserved.
//

#import "YCWXApiUtils.h"
//#import "UIImage+Additional.h"

@implementation YCWXApiUtils
+(YCWXApiUtils *)sharedInstance
{
    static YCWXApiUtils * sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[YCWXApiUtils alloc] init];
    });
    return sharedInstance;
}
- (void)delegateDealloc
{
     self.wxDelegate = nil;
}
//向微信注册
-(BOOL)WXRegister{
    return [WXApi registerApp:@""];
}
//是否安装微信
- (BOOL)isWXAppInstalled
{
    return [WXApi isWXAppInstalled];
}
- (void)notifyWechatOtherResult:(BaseResp *)req
{
    [self onResp:req];
}
//网页类型分享
- (BOOL)sharedLinkToWeChat:(NSString *)title
               description:(NSString *)description
                 detailUrl:(NSString *)detailUrl
                     image:(UIImage *)image
                 shareType:(WXShareSceneType)sharedType
{
    if ([self isWXAppInstalled] == NO) {

        return NO;
    }
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = title;
    message.description = description;
    [message setThumbImage:image];
    WXWebpageObject *webpageObject = [WXWebpageObject object];
    webpageObject.webpageUrl = detailUrl;
    message.mediaObject = webpageObject;
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText= NO;
    req.message = message;
    req.scene = (sharedType == WXShareSceneTypeTimeline? WXSceneTimeline:WXSceneSession);
    BOOL success = [WXApi sendReq:req];
    return success;
}

//分享图片
-(BOOL)shareImageToWeChat:(UIImage *)image
                shareType:(WXShareSceneType)sharedType
{
    if ([self isWXAppInstalled] == NO) {
        
        return NO;
    }
   
    WXMediaMessage *message = [WXMediaMessage message];
    [message setThumbImage:image];
    
    WXImageObject *ext = [WXImageObject object];
    ext.imageData = UIImagePNGRepresentation(image);
    message.mediaObject = ext;
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = (sharedType == WXShareSceneTypeTimeline? WXSceneTimeline:WXSceneSession);
    
    BOOL success = [WXApi sendReq:req];
    return success;
}

/*! @brief 收到一个来自微信的请求，第三方应用程序处理完后调用sendResp向微信发送结果
 *
 * 收到一个来自微信的请求，异步处理完成后必须调用sendResp发送处理结果给微信。
 * 可能收到的请求有GetMessageFromWXReq、ShowMessageFromWXReq等。
 * @param req 具体请求内容，是自动释放的
 */
-(void) onReq:(BaseReq*)req{
    if ([req isKindOfClass:[GetMessageFromWXReq class]]) {
        //微信终端向第三方程序请求提供内容的消息结构体
        if (self.wxDelegate
            && [self.wxDelegate respondsToSelector:@selector(WXApiUtilsDidRecvGetMessageReq:)]) {
            GetMessageFromWXReq *getMessageReq = (GetMessageFromWXReq *)req;
            [self.wxDelegate WXApiUtilsDidRecvGetMessageReq:getMessageReq];
        }
    } else if ([req isKindOfClass:[ShowMessageFromWXReq class]]) {
        //微信通知第三方程序，要求第三方程序显示的消息结构体
        if (self.wxDelegate
            && [self.wxDelegate respondsToSelector:@selector(WXApiUtilsDidRecvShowMessageReq:)]) {
            ShowMessageFromWXReq *showMessageReq = (ShowMessageFromWXReq *)req;
            [self.wxDelegate WXApiUtilsDidRecvShowMessageReq:showMessageReq];
        }
    } else if ([req isKindOfClass:[LaunchFromWXReq class]]) {
        //微信终端打开第三方程序携带的消息结构体
        if (self.wxDelegate
            && [self.wxDelegate respondsToSelector:@selector(WXApiUtilsDidRecvLaunchFromWXReq:)]) {
            LaunchFromWXReq *launchReq = (LaunchFromWXReq *)req;
            [self.wxDelegate WXApiUtilsDidRecvLaunchFromWXReq:launchReq];
        }
    }
}

/*! @brief 发送一个sendReq后，收到微信的回应
 *
 * 收到一个来自微信的处理结果。调用一次sendReq后会收到onResp。
 * 可能收到的处理结果有SendMessageToWXResp、SendAuthResp等。
 * @param resp具体的回应内容，是自动释放的
 */
-(void) onResp:(BaseResp*)resp{
    if ([resp isKindOfClass: [PayResp class]]){
        
    }else if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
        //第三方程序向微信终端发送SendMessageToWXReq后，微信发送回来的处理结果，该结果用SendMessageToWXResp表示。
        if (self.wxDelegate
            && [self.wxDelegate respondsToSelector:@selector(WXApiUtilsDidRecvMessageResponse:)]) {
            SendMessageToWXResp *messageResp = (SendMessageToWXResp *)resp;
            [self.wxDelegate WXApiUtilsDidRecvMessageResponse:messageResp];
        }
    } else if ([resp isKindOfClass:[SendAuthResp class]]) {
        //微信处理完第三方程序的认证和权限申请后向第三方程序回送的处理结果
        if (self.wxDelegate
            && [self.wxDelegate respondsToSelector:@selector(WXApiUtilsDidRecvAuthResponse:)]) {
            SendAuthResp *authResp = (SendAuthResp *)resp;
            [self.wxDelegate WXApiUtilsDidRecvAuthResponse:authResp];
        }
    } else if ([resp isKindOfClass:[AddCardToWXCardPackageResp class]]) {
        //微信返回第三方添加卡券结果
        if (self.wxDelegate
            && [self.wxDelegate respondsToSelector:@selector(WXApiUtilsDidRecvAddCardResponse:)]) {
            AddCardToWXCardPackageResp *addCardResp = (AddCardToWXCardPackageResp *)resp;
            [self.wxDelegate WXApiUtilsDidRecvAddCardResponse:addCardResp];
        }
    }
}
@end
